<template>
    <!-- 对话框表单 -->
    <el-dialog custom-class="ba-operate-dialog" :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form v-if="!baTable.form.loading" ref="formRef" @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items" label-position="right" :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules">
                    <FormItem :label="t('coupon.coupon_name')" type="string" v-model="baTable.form.items!.coupon_name"
                        prop="coupon_name"
                        :input-attr="{ placeholder: t('Please input field', { field: t('coupon.coupon_name') }) }" />
                    <FormItem :label="t('coupon.goods_ids')" type="remoteSelect" v-model="baTable.form.items!.goods_ids"
                        prop="goods_ids"
                        :input-attr="{ field: 'goods_name', 'remote-url': goods + 'index', pk: 'goods.id', multiple: true, placeholder: t('Please select field', { field: t('coupon.goods_ids') }) }" />
                    <FormItem label="可使用类别" type="remoteSelect" v-model="baTable.form.items!.category_ids"
                        prop="category_ids"
                        :input-attr="{ field: 'cate', 'remote-url': category + 'index', pk: 'id', multiple: true, placeholder: t('Please select field', { field: '可使用类别' }) }" />
                    <FormItem label="允许加油" type="radio" v-model="baTable.form.items!.can_oil"
                        prop="can_oil"
                        :data="{ content: { 0: '不允许', 1: '允许' } }"
                        :input-attr="{ placeholder: t('Please select field', { field: t('coupon.can_oil') }) }" />
                    <FormItem :label="t('coupon.coupon_type')" type="radio" v-model="baTable.form.items!.coupon_type"
                        prop="coupon_type"
                        :data="{ content: { 0: t('coupon.coupon_type 0'), 1: t('coupon.coupon_type 1') } }"
                        :input-attr="{ placeholder: t('Please select field', { field: t('coupon.coupon_type') }) }" />
                    <FormItem :label="t('coupon.start_time')" type="date" v-if="baTable.form.items!.coupon_type == 0"
                        v-model="baTable.form.items!.start_time" prop="start_time"
                        :input-attr="{ placeholder: t('Please select field', { field: t('coupon.start_time') }) }" />
                    <FormItem :label="t('coupon.end_time')" type="date" v-if="baTable.form.items!.coupon_type == 0"
                        v-model="baTable.form.items!.end_time" prop="end_time"
                        :input-attr="{ placeholder: t('Please select field', { field: t('coupon.end_time') }) }" />
                    <FormItem :label="t('coupon.days')" type="number" prop="days"
                        v-if="baTable.form.items!.coupon_type == 1" v-model.number="baTable.form.items!.days"
                        :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('coupon.days') }) }" />
                    <FormItem label="派发数量" type="number" prop="num" v-model.number="baTable.form.items!.num"
                        :input-attr="{ step: '0.01', placeholder: '请输入派发数量' }" />
                    <FormItem label="新用户赠送" type="switch" v-model="baTable.form.items!.is_new" prop="is_new" />
                    <FormItem :label="t('coupon.price')" type="number" prop="price"
                        v-model.number="baTable.form.items!.price"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('coupon.price') }) }" />
                    <FormItem :label="t('coupon.condition')" type="number" prop="condition"
                        v-model.number="baTable.form.items!.condition"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('coupon.condition') }) }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
        t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { goods } from '/@/api/controllerUrls'
import { category } from '/@/api/controllerUrls'


const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    coupon_name: [buildValidatorData('required', t('coupon.coupon_name'))],
    coupon_type: [buildValidatorData('required', t('coupon.coupon_type'))],
    price: [buildValidatorData('required', t('coupon.price'))],
    condition: [buildValidatorData('required', t('coupon.condition'))],
})

</script>

<style scoped lang="scss">

</style>
